package cn.shengchao.qywx.infrastrure.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.distribute.cs.UserCs;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeSaleRoleEnum;
import cn.shengchao.examstar.rpc.wecom.dto.UserContactMeQrcodeCheckVo;
import cn.shengchao.examstar.rpc.wecom.req.CpUserInvitePageQuery;
import cn.shengchao.examstar.rpc.wecom.resp.CpUserCustomerVo;
import cn.shengchao.qywx.infrastrure.po.QywxUser;
import cn.shengchao.qywx.infrastrure.po.QywxUserSysbind;
import cn.shengchao.qywx.infrastrure.service.QywxUserService;
import cn.shengchao.qywx.infrastrure.service.QywxUserSysbindService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.shengchao.qywx.infrastrure.po.QywxUserServiceInvite;
import cn.shengchao.qywx.infrastrure.mapper.QywxUserServiceInviteMapper;
import cn.shengchao.qywx.infrastrure.service.QywxUserServiceInviteService;

@Service
public class QywxUserServiceInviteServiceImpl extends ServiceImpl<QywxUserServiceInviteMapper, QywxUserServiceInvite> implements QywxUserServiceInviteService {
    @Autowired
    private QywxUserSysbindService qywxUserSysbindService;
    @Autowired
    private QywxUserService qywxUserService;

    @Override
    public void savePlatUserServiceInviteRecord(Long userId) {
        QywxUserServiceInvite qywxUserServiceInvite=new QywxUserServiceInvite();
        qywxUserServiceInvite.setUserId(userId);
        qywxUserServiceInvite.setServiceUserId(UserCs.PLATFORM_USER_ID);
        qywxUserServiceInvite.setServiceDistributeCode(DistributeSaleRoleEnum.ROLE_YSXG.getCode());
        qywxUserServiceInvite.setServiceDistributeUserId(UserCs.PLATFORM_USER_ID);
        qywxUserServiceInvite.setServiceCpUserId("");
        qywxUserServiceInvite.setDataStatus(0);
        baseMapper.insert(qywxUserServiceInvite);
    }

    @Override
    public PageResult<CpUserCustomerVo> queryCpUserInvitePage(CpUserInvitePageQuery pageQuery) {
        Page<Object> page = PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<CpUserCustomerVo> data = baseMapper.queryCpUserInvitePage(pageQuery);
        return PageResult.of(page.getTotal(), data);
    }

    @Override
    public UserContactMeQrcodeCheckVo queryCpUserCheckVo(Long reqUserId) {
        QywxUserServiceInvite qywxUserServiceInvite = baseMapper.selectOne(Wrappers.lambdaQuery(QywxUserServiceInvite.class)
                .eq(QywxUserServiceInvite::getUserId, reqUserId)
                .eq(QywxUserServiceInvite::getDataStatus, 0)
        );
        //没有找到邀请关系
        if (qywxUserServiceInvite == null) {
            return null;
        } else {

            //可以出码
            UserContactMeQrcodeCheckVo userContactMeQrcodeCheckVo = baseMapper.queryCpUserCheckVo(reqUserId);
            if (userContactMeQrcodeCheckVo != null) {
                userContactMeQrcodeCheckVo.setUserId(reqUserId);
                userContactMeQrcodeCheckVo.setServiceUserId(qywxUserServiceInvite.getServiceUserId());
                userContactMeQrcodeCheckVo.setServiceCpUserId(qywxUserServiceInvite.getServiceCpUserId());
                userContactMeQrcodeCheckVo.setStatus(1);
                return userContactMeQrcodeCheckVo;
            } else {
                List<QywxUserSysbind> qywxUserSysbindList1 = qywxUserSysbindService.list(Wrappers.lambdaQuery(QywxUserSysbind.class).eq(QywxUserSysbind::getSysUserId, qywxUserServiceInvite.getServiceUserId()));
                if (CollUtil.isNotEmpty(qywxUserSysbindList1)) {
                    List<String> distributeCpUserIdList = qywxUserSysbindList1.stream().map(QywxUserSysbind::getQywxUserId).toList();
                    //找出本人有码的cp成员
                    List<QywxUser> qywxUserList = qywxUserService.list(Wrappers.lambdaQuery(QywxUser.class).eq(QywxUser::getStatus, "1").eq(QywxUser::getQrCodeStatus, 1).in(QywxUser::getUserId, distributeCpUserIdList));
                    if (CollUtil.isNotEmpty(qywxUserList)) {
                        QywxUser qywxUser = qywxUserList.getFirst();
                        UserContactMeQrcodeCheckVo userContactMeQrcodeCheckVo1 = new UserContactMeQrcodeCheckVo();
                        userContactMeQrcodeCheckVo1.setUserId(reqUserId);
                        userContactMeQrcodeCheckVo1.setServiceUserId(qywxUserServiceInvite.getServiceUserId());
                        userContactMeQrcodeCheckVo1.setServiceCpUserId(qywxUserServiceInvite.getServiceCpUserId()==null?qywxUser.getUserId():qywxUserServiceInvite.getServiceCpUserId());
                        userContactMeQrcodeCheckVo1.setServiceCpQrcode(qywxUser.getQrCodeUrl());
                        userContactMeQrcodeCheckVo1.setStatus(0);
                        return userContactMeQrcodeCheckVo1;
                    }
                } else {
                    //找出上级有码的cp成员
                    List<QywxUserSysbind> qywxUserSysbindList2 = qywxUserSysbindService.list(Wrappers.lambdaQuery(QywxUserSysbind.class).eq(QywxUserSysbind::getSysUserId, qywxUserServiceInvite.getServiceDistributeUserId()));
                    if (CollUtil.isNotEmpty(qywxUserSysbindList2)) {
                        List<String> distributeCpUserIdList = qywxUserSysbindList2.stream().map(QywxUserSysbind::getQywxUserId).toList();
                        List<QywxUser> qywxUserList = qywxUserService.list(Wrappers.lambdaQuery(QywxUser.class).eq(QywxUser::getStatus, "1").eq(QywxUser::getQrCodeStatus, 1).in(QywxUser::getUserId, distributeCpUserIdList));
                        if (CollUtil.isNotEmpty(qywxUserList)) {
                            QywxUser qywxUser = qywxUserList.getFirst();
                            UserContactMeQrcodeCheckVo userContactMeQrcodeCheckVo1 = new UserContactMeQrcodeCheckVo();
                            userContactMeQrcodeCheckVo1.setUserId(reqUserId);
                            userContactMeQrcodeCheckVo1.setServiceUserId(qywxUserServiceInvite.getServiceUserId());
                            userContactMeQrcodeCheckVo1.setServiceCpUserId(qywxUserServiceInvite.getServiceCpUserId());
                            userContactMeQrcodeCheckVo1.setDistributeUserId(qywxUserServiceInvite.getServiceDistributeUserId());
                            userContactMeQrcodeCheckVo1.setDistributeCpUserId(qywxUser.getUserId());
                            userContactMeQrcodeCheckVo1.setDistributeCpUserQrcode(qywxUser.getQrCodeUrl());
                            userContactMeQrcodeCheckVo1.setStatus(2);
                            return userContactMeQrcodeCheckVo1;
                        }
                    }
                }
            }


        }
        return null;
    }
}
